Unit BSQLGeneratorUnit;

{$mode objfpc}{$H+}

Interface

Uses
  Classes, SysUtils, strutils, BCommonUnit, BGeneratorUnit, BPeaUnit;

Type

{ BSQLGeneratorClass }

 BSQLGeneratorClass = Class(BGeneratorClass)
  Private
    bPeas: BPeasList;
    Procedure UpdatePeaPluralProperties(Const aProperty: BPeaPropertyClass;
      Var aIndex: Integer);
    Procedure UpdateHolderPluralProperties(Const aOwnerPea: BPeaClass;
      Const aProperty: BPeaPropertyClass; Var aIndex: Integer);
    Procedure UpdatePea;
    Procedure UpdatePeaHolder;
  Public
    Procedure Update; override;
    Procedure SavePeaUnit; override;
    Constructor Build(Const aPeaPath: String; Const aPea: BPeaClass;
      Const aPeas: BPeasList; Const aProjectName: String);
End;

Implementation

ResourceString
  rsSQLDir = 'SQL/';
  mskPeaSQLUnit = 'b%sclass.sql';

{ BSQLGeneratorClass }

Procedure BSQLGeneratorClass.UpdatePeaPluralProperties(
  Const aProperty: BPeaPropertyClass;
  Var aIndex: Integer);
Begin
  With aProperty Do
    Begin
      AddLine('CREATE TABLE %s_%s(',
        [UpCase(Self.Pea.Caption), UpCase(SQLCaption)], 0, aIndex);
      AddLine('%s INTEGER NOT NULL,', [UpCase(Self.Pea.Caption)], 2, aIndex);
      AddLine('%s %s NOT NULL',[UpCase(Caption), KindSQL], 2, aIndex);
      AddLine(');', 0, aIndex);
      AddEmptyLine(aIndex);

      AddLine('CREATE INDEX IDX_%0:s_%1:s_MAIN ON %0:s_%1:s (%0:s);',
        [UpCase(Self.Pea.Caption), SQLCaption], 0, aIndex);
      AddEmptyLine(aIndex);
    End;
End;

Procedure BSQLGeneratorClass.UpdateHolderPluralProperties(
  Const aOwnerPea: BPeaClass; Const aProperty: BPeaPropertyClass;
  Var aIndex: Integer);
Begin
  With aProperty Do
    Begin
      AddLine('CREATE TABLE %s_%s(',
        [UpCase(Self.Pea.Caption), UpCase(SQLCaption)], 0, aIndex);
      AddLine('%s INTEGER NOT NULL,', [UpCase(Self.Pea.Caption)], 2, aIndex);
      AddLine('%s %s NOT NULL,',[UpCase(Caption), KindSQL], 2, aIndex);
      AddLine(');', 0, aIndex);
      AddEmptyLine(aIndex);

      AddLine('CREATE INDEX IDX_%0:s_%1:s_MAIN ON %0:s_%1:s(%0:s);',
        [UpCase(Self.Pea.Caption), SQLCaption], 0, aIndex);
    End;
End;

Procedure BSQLGeneratorClass.UpdatePea;
Var
  i: Integer;
  aIndex: Integer = 0;
Begin
  AddLine('CREATE TABLE %s(', [UpCase(Pea.Caption)], 0, aIndex);
  If Pea.Kind = pkHolder Then
    Begin
      AddLine('%s INTEGER NOT NULL,',[UpCase(Pea.HoldingPea.Caption)],2,aIndex);
      AddLine('%s INTEGER NOT NULL,',[UpCase(Pea.Caption)], 2, aIndex);
    End
  Else
    Begin
      AddLine('ID INTEGER NOT NULL,', 2, aIndex);
      AddLine('DELETED INTEGER NOT NULL,', 2, aIndex);
      If Pea.Kind = pkTree Then
        AddLine('PARENT INTEGER NOT NULL,', 2, aIndex);
      AddLine('PRIOR INTEGER NOT NULL,', 2, aIndex);
      AddLine('NEXT INTEGER NOT NULL,', 2, aIndex);
    End;
  For i := 0 To Pea.Properties.Count - 1 Do
    If Not(Pea[i].IsPlural) Then
      AddLine('%s %s NOT NULL,', [Pea[i].SQLCaption,Pea[i].KindSQL], 2, aIndex);
  AddLine('CONSTRAINT PK_%s_ID PRIMARY KEY (ID)',
    [UpCase(Pea.Caption)], 2, aIndex);
  AddLine(');', 0, aIndex);
  AddEmptyLine(aIndex);

  AddLine('CREATE GENERATOR GEN_%0:s_ID;', [UpCase(Pea.Caption)], 0, aIndex);
  AddEmptyLine(aIndex);

  For i := 0 To Pea.Properties.Count - 1 Do
    If (Pea[i].IsPlural) And Not(Pea[i].Kind = ppkHolders) Then
      UpdatePeaPluralProperties(Pea[i], aIndex);
End;

Procedure BSQLGeneratorClass.UpdatePeaHolder;
Var
  i, j: Integer;
  aIndex: Integer = 0;
  aProperty: BPeaPropertyClass;
Begin
  For i := 0 To bPeas.Count - 1 Do
    If bPeas[i].HasHolder(Pea) Then
      Begin
        aProperty := bPeas[i].GetHolder(Pea);
        AddLine('CREATE TABLE %s_%s(',
          [UpCase(bPeas[i].Caption), aProperty.SQLCaption], 0, aIndex);
        AddLine('%s INTEGER NOT NULL,', [UpCase(bPeas[i].Caption)], 2, aIndex);
        AddLine('%s INTEGER NOT NULL,', [UpCase(Pea.HoldingPea.Caption)], 2,
          aIndex);
        For j := 0 To Pea.Properties.Count - 1 Do
          If Not(Pea[j].IsPlural) Then
            AddLine('%s %s NOT NULL' + IfThen(j=Pea.Properties.Count-1,'',','),
              [Pea[j].SQLCaption,Pea[j].KindSQL], 2, aIndex);
        AddLine(');', 0, aIndex);
        AddEmptyLine(aIndex);

        For j := 0 To Pea.Properties.Count - 1 Do
          If (Pea[j].IsPlural) And Not(Pea[j].Kind = ppkHolders) Then
            UpdateHolderPluralProperties(bPeas[i], Pea[j], aIndex);
      End;
End;

Procedure BSQLGeneratorClass.Update;
Begin
  Case Pea.Kind Of
    pkPlain, pkTree: UpdatePea;
    pkHolder: UpdatePeaHolder;
  End;
End;

Procedure BSQLGeneratorClass.SavePeaUnit;
Begin
  bUnitFile.SaveToFile(bPeasPath + rsSQLDir +
    LowerCase(Format(mskPeaSQLUnit, [Pea.Caption])));
End;

Constructor BSQLGeneratorClass.Build(Const aPeaPath: String;
  Const aPea: BPeaClass; Const aPeas: BPeasList; Const aProjectName: String);
Begin
  Inherited Build(aPeaPath, aProjectName);
  bPea := aPea;
  bPeas := aPeas;
End;

End.

